package com.bcst.service.impl;

import com.bcst.dao.AccountDao;
import com.bcst.dao.JdbcAccountDaoImpl;
import com.bcst.entity.Account;
import com.bcst.service.TransferService;
import com.bcst.utils.ConnectionUtils;
import com.bcst.utils.TransactionManager;

import java.sql.Connection;

/*
 *@description 欢迎关注公众号 编程识堂,每日更新技术相关文章，关注我不迷路
 *@author 小堂
 *@create 2023/9/2 16:13
 */
public class TransferServiceImpl implements TransferService {

    private AccountDao accountDao;

    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    public Boolean transfer(String fromCardNo, String toCardNo, int money)
            throws Exception {
      /*  try {
            TransactionManager.getInstance().beginTransaction();*/
            Account from = accountDao.queryAccountByCardNo(fromCardNo);
            Account to = accountDao.queryAccountByCardNo(toCardNo);
            from.setMoney(from.getMoney()-money);
            to.setMoney(to.getMoney()+money);
            accountDao.updateAccountByCardNo(from);
            //制造异常
            int c = 100/0;
            accountDao.updateAccountByCardNo(to);
          //  TransactionManager.getInstance().commit();
            return true;
        /*} catch (Exception e) {
            e.printStackTrace();
            TransactionManager.getInstance().rollback();
            throw new Exception(e);
        }*/
    }
}

